﻿using System.ComponentModel;
using EnumsNET;
namespace AppTemplate.Infrastructure.ExtendMethods
{
    public static class EnumExtendMethod
    {
        public static string? GetDescription(this Enum @enum)
        {
            var attrs = Enums.GetAttributes(@enum.GetType(), @enum);
            var desc = attrs?.FirstOrDefault(a => a.GetType() == typeof(DescriptionAttribute)) as DescriptionAttribute;
            return desc?.Description;
        }

        public static List<EnumItem> GetEnumItems(this Enum @enum)
        {
            List<EnumItem> result = new List<EnumItem>();
            var members = Enums.GetMembers(@enum.GetType());
            foreach ( var member in members )
            {
                EnumItem item = new EnumItem();
                item.Name = member.Name;

                var descAttr = member.Attributes.FirstOrDefault(a=>a.GetType()==typeof(DescriptionAttribute));
                item.Description = descAttr != null ? ((DescriptionAttribute)descAttr).Description : member.Name;
                result.Add(item);
            }
            return result;
        }
    }
}
